package studentsinfomanagement.main;

import studentsinfomanagement.bean.Department;
import studentsinfomanagement.bean.Student;
import studentsinfomanagement.bean.Teacher;
import studentsinfomanagement.bean.User;
import studentsinfomanagement.dao.AdministratorDAO;
import studentsinfomanagement.dao.DepartmentDAO;
import studentsinfomanagement.dao.StudentDAO;
import studentsinfomanagement.dao.TeacherDAO;
import studentsinfomanagement.util.DBUtil;
import studentsinfomanagement.view.LoginView;

/**
 * 逻辑层类
 *  管理员账号：admin
 *  密码：123
 */
public class Main {
    private static DBUtil dbUtil;//数据库工具类单例对象
    private static LoginView loginView;//视图层对象
    /**
     * 初始化数据库
     */
    public static void initDB(){
        dbUtil = DBUtil.getDBUtil();
        //检查数据库是否初始化
        if (dbUtil.exeute("select from teacher where id = '1'") &&
                dbUtil.exeute("select 1 from  student where id = '1'")) {
            return;
        }
        //初始化数据库
        dbUtil.exeute("insert into teacher( name, tno, dno, sex, password) " +
                "values('teacher wang', 'admin', '001','F','admin')");

        dbUtil.exeute("insert into student( name, sno, dno, sex, password) " +
                "values('student wang', 'admin', '001','F','admin')");
    }

    /**
     * 程序的入口
     * @param args
     */
    public static void main(String[] args){
        //1.初始化数据库
        initDB();
        //2.显示界面
        Main main = new Main();
        loginView = new LoginView(main);
    }

    /**
     * 登录系统
     * @param type 身份 0-学生 1-老师
     * @param accNum 账号
     * @param password 密码
     * @return 登录结果
     */
    public boolean loginSystem(int type, String accNum, String password){
        boolean result = false;
        if(type == 0){//学生登录系统
            //1.获取学生数据操作对象
            StudentDAO studentDAO = StudentDAO.getInstance();
            //2.调用学生数据层登录方法
            result = studentDAO.queryForLogin(accNum,password);
        }else if(type == 1){//老师登录系统
            //1.获取老师数据层操作对象
            TeacherDAO teacherDAO = TeacherDAO.getInstance();
            //2.调用老师数据层登录系统的方法
            result = teacherDAO.queryForLogin(accNum,password);
        }else{//管理员登录系统
            //1.获取管理员数据层操作对象
            AdministratorDAO administratorDAO = AdministratorDAO.getInstance();
            //2.调用管理员数层登录系统的方法
            result = administratorDAO.queryForLogin(accNum,password);
        }
        return result;
    }

    /**
     * 注册用户
     * @param user 用户对象
     * @param type 类型 0-学生 1-老师
     * @return 注册结果
     */
    public boolean register(User user, int type){
        DepartmentDAO departmentDAO = DepartmentDAO.getInstance();
        if(type == 0){//学生
            Student student = (Student)user;
            //1.获取学生数据操作对象
            StudentDAO studentDAO = StudentDAO.getInstance();
            //2.转换专业名为专业号
            String department = student.getDepartment();
            Department depResult = departmentDAO.getDepartmentByName(department);
            student.setDepartment(depResult.getDno());
            //3.注册学生账号
            boolean addResult = studentDAO.add(student);
            return addResult;
        }else{//老师
            Teacher teacher = (Teacher) user;
            //1.获取老师数据操作对象
            TeacherDAO teacherDao  = TeacherDAO.getInstance();
            //2.转换专业名为专业号
            String department = teacher.getDepartment();
            Department depResult = departmentDAO.getDepartmentByName(department);
            teacher.setDepartment(depResult.getDno());
            //3.注册老师账号
            boolean addResult = teacherDao.add(teacher);
            return addResult;
        }
    }
}
